package com.lebang.medicalInquiry.service;


import com.lebang.medicalInquiry.constant.BusinessEnum;
import com.lebang.medicalInquiry.exception.BusinessException;
import com.lebang.medicalInquiry.mapper.*;
import com.lebang.medicalInquiry.model.po.Organization;

import com.lebang.medicalInquiry.model.po.PhysicianOrganizationRelevance;
import com.lebang.medicalInquiry.model.po.User;
import com.lebang.medicalInquiry.model.vo.OrganizationOperationListVo;
import com.lebang.medicalInquiry.model.vo.PageVo;
import com.lebang.medicalInquiry.model.vo.admin.OrganizationListVo;
import com.lebang.medicalInquiry.model.vo.admin.PhysicianListVo;
import com.lebang.medicalInquiry.model.vo.admin.PhysicianOrganizationReVo;
import com.lebang.medicalInquiry.utils.TextUtil;
import com.lebang.medicalInquiry.utils.UUIDUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class OrganizationService {

    @Resource
    private OrganizationMapper organizationMapper;

    @Resource
    private PhysicianMapper physicianMapper;

    @Resource
    private UserMapper userMapper;

//    @Resource
//    private OrganizationOperationMapper organizationOperationMapper;

    @Resource
    private PhysicianOrganizationRelevanceMapper physicianOrganizationRelevancem;


    public PageVo<List<OrganizationListVo>> getList(String keyWord, Integer state, Integer page, Integer limit) {

        return new PageVo<List<OrganizationListVo>>(organizationMapper.getList(keyWord, state, (page - 1) * limit, limit), organizationMapper.count(keyWord, state));
    }

    public void operator(Organization po) {
        if (po.getId() == null) {
            po.setCreateTime(LocalDateTime.now());
            po.setUpdateTime(LocalDateTime.now());
            po.setState(1);
            organizationMapper.insertSelective(po);

        } else {
            po.setUpdateTime(LocalDateTime.now());
            organizationMapper.updateByPrimaryKey(po);
        }
    }

    public void remove(Integer organizationId) {

        organizationMapper.deleteByPrimaryKey(organizationId);

    }

    public PageVo<List<OrganizationOperationListVo>> getOperaList(Integer organizationId, String keyWord, Integer state, Integer page, Integer limit) {

        return new PageVo<>(userMapper.organizationByCondition(organizationId, keyWord, state, (page - 1) * limit, limit),
                userMapper.organizationCountByCondition(organizationId, keyWord, state));
    }

    public void operatorOpera(OrganizationOperationListVo po) {
        String account = po.getPhoneNumber();

        if (po.getId() == null) {
            User byPhone = userMapper.getByPhone(account);
            if (byPhone != null) {
                throw new BusinessException("重复账号");
            }
            final User user = new User();
            user.setCreateTime(LocalDateTime.now());
            user.setUpdateTime(LocalDateTime.now());
            user.setState(1);
            user.setUuid(UUIDUtil.L());
            user.setOrganizationId(po.getOrganizationId());
            user.setRole(BusinessEnum.ROLE_OPERATION.getName());
            userMapper.insertSelective(user);

        } else {
            User byPhone = userMapper.getByPhone(account);
            if (byPhone != null && !byPhone.getId().equals(po.getId())) {
                throw new BusinessException("重复账号");
            }
            User user = new User();
            user.setId(po.getId());
            user.setPhoneNumber(po.getPhoneNumber());
            user.setState(1);
            user.setId(po.getId());
            user.setOrganizationId(po.getOrganizationId());
            user.setUpdateTime(LocalDateTime.now());
            userMapper.updateByPrimaryKey(user);
        }
    }

    public void removeOpera(Integer organizationOperaId) {
        User user = userMapper.selectByPrimaryKey(organizationOperaId);
        userMapper.deleteByPrimaryKey(organizationOperaId);
        // 删除分配记录
        physicianOrganizationRelevancem.removeByOUuid(user.getUuid());

    }

    /**
     * 给运营人员分配医师列表
     *
     * @param physicianIds
     * @param operatorId
     */
    public void allocationPhysician(String physicianIds, Integer operatorId) {


        final User user = userMapper.selectByPrimaryKey(operatorId);
        // 筛选是否重复选择的医师
//        Set<Integer> existIdList = physicianOrganizationRelevancem.getPIdsByOpeartorUuid(organizationOperation.getUuid());
        physicianOrganizationRelevancem.removeByOUuid(user.getUuid());
        if (TextUtil.isEmpty(physicianIds)) {
            return;
        }
        Set<Integer> physicianIdList = Arrays.stream(physicianIds.split(",")).map(Integer::parseInt).collect(Collectors.toSet());
        if (physicianIdList.size() > 0) {
            physicianOrganizationRelevancem.addAllocationPhysician(user.getOrganizationId(), user.getUuid(), physicianIdList);
        }

    }

    public List<PhysicianOrganizationReVo> getAllPhysician() {
        return physicianMapper.getOrganizationPhysicianList();
    }
}
